public with sharing class WBLeadCommnetController {

    public Case cas  {get;set;}
    public Lead lead {get;set;}
    
    public WBLeadCommnetController(){
            
            String caseId = getParameter('caseid');
            String leadId = getParameter('leadid');
            
            try{
                if( isNotBlank( caseId ) ){
                        cas = new Case();
                        cas = [Select c.Weibo_User__c, c.Id, c.Description__c From Case c WHERE ID=:caseId ];
                }else if( isNotBlank( leadId ) ){
                        lead = new Lead();
                        // lead = [SELECT ]
                }
            }catch(Exception ex){
                    system.debug('@@@@@@@@@@  ERROR : '+ex);
            }
    }
    
     public PageReference replyComment() {
        
        String jsonResult = '';
        String paramWbid 	= getParameter('wbid');
    	String paramCaseId 	= getParameter('caseid');
    	String content 		= getParameter('wb_content');
    	
        WBComment__c comment = new WBComment__c();
        comment.text__c = content;
        comment.mid__c = paramWbid;
        comment.source__c ='Salesforce';
        comment.Case__c = paramCaseId;
        
        system.debug('@@@@@@@@@@@@@@@@@@@@@@@@@@  ID :  '+ comment.Case__c);
        system.debug('@@@@@@@@@@@@@@@@@@@@@@@@@@  ID :  '+ comment.mid__c);
        system.debug('@@@@@@@@@@@@@@@@@@@@@@@@@@  CN :  '+ content); 
         
        if( comment.mid__c!=null && comment.mid__c.length()>0 ){
        	
            // Send Weibo
            WB_OpenApi.WB_Comment_Create_In wbc_in = new WB_OpenApi.WB_Comment_Create_In();
            wbc_in.id = comment.mid__c;
            wbc_in.comment = comment.text__c;
            jsonResult = wbc_in.toRequestURL();
            // Insert Weibo
            insert comment;
            
            system.debug('@@@@@@@@@@@@@@@@@@@@@@@@@@  :  '+ content);
            system.debug('@@@@@@@@@@@@@@@@@@@@@@@@@@  :  '+ jsonResult);
        }
        system.debug('@@@@@@@@@@@@@@@@@@@@@@@@@@ isSuccess :  '+ jsonResult.contains( comment.text__c)  );
        
        return null;
     }

    public List<KnowledgeArticleVersion> getQueryArticals(){
            
            String caseId = getParameter('caseid');
            List<KnowledgeArticleVersion> listArticle = new List<KnowledgeArticleVersion>();
                    
            if( isNotBlank( caseId ) ){
                    List<CaseArticle> lstCA = [Select c.KnowledgeArticle.Id From CaseArticle c WHERE c.CaseId=:caseId limit 1];
                    
                   if( lstCA!=null && lstCA.size()>0 ){
                                CaseArticle ca = lstCA[0];
                                
                                String kaid = ca.KnowledgeArticle.ID;
                        listArticle = [ SELECT ID,Title, Summary FROM KnowledgeArticleVersion 
                                                                 WHERE PublishStatus='Online' AND Language = 'en_US'
                                                                         AND KnowledgeArticleId=:kaid                               ];
                    }
            }
            return listArticle;
    }
    
    private boolean isNotBlank( String val ){
            boolean retVal = false;
            if( val !=null && val.length()>0 ){
                    retVal = true;
            }
            return retVal;
    }
    
    private String getParameter( String paramName ){
            String paramVal = null;
            if( paramName!=null && paramName.length()>0 ){
                    paramVal = ApexPages.currentPage().getParameters().get( paramName );
            }
            return paramVal;
    }

}